// npm 包
import React, { useRef } from 'react';
import type { ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { ModalForm } from '@ant-design/pro-form';
import { Form, message } from 'antd';
// 数据请求
import {
  listFileUploadTaskForCurrentUser,
  cancelTaskById,
  reExecuteTaskById,
  updateFileUploadTime,
} from './service';

interface SelectFormProps {
  modalVisible: boolean;
  onCancel: () => void;
}

const CANCEL_TYPE = 0;
const DOWNLOAD_TYPE = 2;
const REDOWNLOAD_TYPE = 3;

const DownLoadModal: React.FC<SelectFormProps> = (props) => {
  const { modalVisible, onCancel } = props;
  const [form] = Form.useForm();
  const actionRef = useRef<any>();
  const onReload = () => {
    actionRef.current.reload();
  };
  const LabelStatusMapHandler = (value: any) => {
    console.log(value);
    const { taskStatus, id, fileUrl, projectType } = value;
    switch (taskStatus) {
      case CANCEL_TYPE:
        return (
          <a
            onClick={async () => {
              const res = await cancelTaskById(id, projectType);
              if (res.code == 200) {
                message.success('取消成功');
                onReload();
              } else {
                message.error(res.message);
              }
            }}
          >
            取消
          </a>
        );
      case DOWNLOAD_TYPE:
        return (
          <a
            target="_blank"
            href={fileUrl}
            onClick={async () => {
              const res = await updateFileUploadTime(id, projectType);
              if (res.code == 200) {
                onReload();
              } else {
                message.error(res.message);
              }
            }}
            rel="noreferrer"
          >
            下载
          </a>
        );
      case REDOWNLOAD_TYPE:
        return (
          <a
            onClick={async () => {
              const res = await reExecuteTaskById(id, projectType);
              if (res.code == 200) {
                message.success('重新下载成功');
                onReload();
              } else {
                message.error(res.message);
              }
            }}
          >
            重新下载
          </a>
        );
      default:
        break;
    }
  };

  const columns: ProColumns<{}>[] = [
    {
      title: '文件名',
      dataIndex: 'fileName',
      hideInForm: true,
    },
    {
      title: '下载时间',
      dataIndex: 'downloadTimeStr',
      hideInForm: true,
    },
    {
      title: '下载状态',
      dataIndex: 'taskStatusDesc',
    },
    {
      title: '操作人',
      dataIndex: 'username',
    },
    {
      title: '操作时间',
      dataIndex: 'createTimeStr',
    },
    {
      title: '操作',
      dataIndex: 'opretor',
      render: (_: any, value: any) => LabelStatusMapHandler(value),
    },
  ];

  return (
    <ModalForm

      title="下载列表"
      visible={modalVisible}
      modalProps={{
        onCancel,
        destroyOnClose: true,

      }}
      form={form}
      width="80%"
      className="select-activity-modal"
      submitter={{
        render: () => null,
      }}
    >
      <ProTable<{}>
        rowKey="id"
        actionRef={actionRef}
        request={async (params) => {
          const msg = await listFileUploadTaskForCurrentUser({ ...params, pageNo: params.current });
          console.log(msg)
          return {
            data: msg.result.records,
            success: true,
            total: msg.result.total,
          };
        }}
        columns={columns}
        search={false}
      />
    </ModalForm>
  );
};

export default DownLoadModal;
